package com.sbj.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbj.dto.req.CarouselReq;
import com.sbj.dto.res.CarouselRes;
import com.sbj.entity.Carousel;
import com.sbj.mapper.CarouselMapper;
import com.sbj.service.CarouselService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/9 16:48
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Override
    public Page<CarouselRes> lists(CarouselReq carouselReq) {
        Page<Carousel> page=new Page<>(carouselReq.getPageNum(),carouselReq.getPageSize());
        LambdaQueryWrapper<Carousel> carouselLambdaQueryWrapper = Wrappers.lambdaQuery(Carousel.class).eq(StrUtil.isNotEmpty(carouselReq.getName()), Carousel::getName, carouselReq.getName())
                .orderByAsc(Carousel::getCreateTime);
        return Convert.convert(new TypeReference<Page<CarouselRes>>() {},this.page(page,carouselLambdaQueryWrapper));
    }
}
